package cz.acies.uniql.dialect;

import java.util.*;

public class ColumnTypes extends HashMap<Integer, ColumnTypesElement> 
{

	private static final long serialVersionUID = -2783313472758211659L;

	public static void main(String[] args) {}

	//----------------------- Construct methods --------------------------------
		
	public ColumnTypes() {
		super();
	}	

	public ColumnTypesElement registerColumnType(int type, ColumnTypesElement element) {
		Integer key = new Integer(type);
		put(key, element);
		return (ColumnTypesElement)get(key);
	}

	public ColumnTypesElement registerColumnType(int type, String typeName) {
		ColumnTypesElement element = new ColumnTypesElement(typeName);
		return this.registerColumnType(type, element);
	}

	public ColumnTypesElement registerColumnType(int type, String typeName, int length){
		ColumnTypesElement element = new ColumnTypesElement(typeName, length);
		return this.registerColumnType(type, element);			
	}

	public ColumnTypesElement registerColumnType(int type, String typeName, int length, int scale){
		ColumnTypesElement element = new ColumnTypesElement(typeName, length, scale);
		return this.registerColumnType(type, element);		
	}
	
	// ----- Getter & setter methods ------------------------------------------	

	public ColumnTypesElement getColumnType(String key){
		return (ColumnTypesElement)get(key);
	}

	public String getColumnTypeName(int type) {
		//System.out.println("ColumnTypes.getColumnTypeName() = "+this);
		//System.out.println("ColumnTypes.getColumnTypeName().type = "+type);
		Integer key = new Integer(type);
		ColumnTypesElement element = (ColumnTypesElement)get(key);
		return element.getTypeName();
	}
	
	public String getColumnTypeName(String type){
		Integer key = new Integer(type);
		ColumnTypesElement element = (ColumnTypesElement)get(key);
		return element.getTypeName();
	}
	
	public static String parse(String type, Integer length, Integer scale) {
		if (type.indexOf("(") < 0) {
			return type;
		} else {
			if (type.indexOf("$l") > -1) {
				String l = length.toString();
				type = type.substring(0, type.indexOf("$l"))+l+")".toLowerCase();
			} else if (type.indexOf("$p,$s") > -1) {
				
				int s = (scale != null) ? scale.intValue() : 0;
				int p = (length != null) ? length.intValue() : 0;
				if (p > 38) p = 38;
				if (p < 15) p = 15;
				if (p <= s) { p = s; s = s - 1; }
				
				type = type.substring(0, type.indexOf("$p,$s"))+p+","+s+")";
				//System.out.println("$p,$s: "+type);
			}
		}
		return type; 		
	}	
};
/*
class ColumnTypesElement extends Hashtable 
{
	public ColumnTypesElement(){
		super();
	} 

	public ColumnTypesElement(String typeName){
		super();
		setTypeName(typeName);
	} 

	public ColumnTypesElement(String typeName, int length){
		super();
		setTypeName(typeName);
		setLength(length);
	} 

	public ColumnTypesElement(String typeName, int length, int scale){
		super();
		setTypeName(typeName);
		setLength(length);
		setScale(scale);
	} 
		
	// -----  ------------------  ---------------------------------------------
	
	public void setTypeName(String value){
		String key = "typeName";
		put(key, value);
	}
		
	public String getTypeName(){
		return (String)get("typeName");		
	}

	public void setLength(int length){
		String key = "lenght"; 
		put(key, new Integer(length));
	}

	public void setLength(String value){
		String key = "lenght";
		put(key, new Integer(value));
	}

	public Integer getLength(){
		return (Integer)get("lenght");		
	}
	
	public void setScale(int length){
		String key = "scale"; 
		put(key, new Integer(length));
	}

	public void setScale(String value){
		String key = "scale";
		put(key, new Integer(value));
	}

	public Integer getScale(){
		return (Integer)get("scale");		
	}
	
	public void setDefault(String value){
		String key = "default";
		put(key, value);
	}
		
	public String getDefault(){
		return (String)get("default");		
	}

};
*/